﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SPMS.Common;
using SPMS.Data;
using SPMS.Web.Models;

namespace SPMS.Web.Controllers
{
    /// <summary>
    /// 组织结构控制器。
    /// </summary>
    public class OrganizationController : Controller
    {
        #region ---Views

        /// <summary>
        /// 默认视图动作.
        /// </summary>
        /// <returns>视图结果。</returns>
        public ActionResult Index()
        {
            return View();
        }

        #endregion

        #region ---Actions

        /// <summary>
        /// 获得组织结构信息列表。
        /// </summary>
        /// <param name="queryModel">查询模型对象。</param>
        /// <returns>组织结构信息列表。</returns>
        [JsonHandleError]
        public JsonResult List()
        {
            //判断组织结构模型是否已经初始化
            if (null == s_Organizations)
            {
                //创建上下文
                using (AccountFileManagementEntities context = new AccountFileManagementEntities())
                {
                    //查询数据
                    var queryOrganization = from item in context.Organizations
                                            orderby item.Parent_ID, item.Organization_Code
                                            select item;

                    //用于保存父节点
                    OrganizationModel parentNode = null;

                    //用于保存节点
                    OrganizationModel node = null;

                    //用于保存节点映射
                    Dictionary<Guid, OrganizationModel> nodeMapping = new Dictionary<Guid, OrganizationModel>();

                    //用于保存节点
                    List<OrganizationModel> nodes = new List<OrganizationModel>();

                    //循环添加节点
                    foreach (Organization item in queryOrganization)
                    {
                        //判断是否有父节点
                        if (!item.Parent_ID.HasValue && !item.IsLeaf)
                        {
                            //创建节点
                            node = new OrganizationModel()
                            {
                                id = item.Organization_ID,
                                code = item.Organization_Code,
                                text = item.Organization_Code + " " + item.Organization_Name,
                                name = item.Organization_Name,
                                abbr = item.Organization_Abbr,
                                children = new List<TreeNodeModel>(),
                                type = item.Organization_Type,
                                desc = null,
                                expanded = true,
                                leaf = item.IsLeaf
                            };

                            //添加到节点
                            nodes.Add(node);

                            //添加到节点映射
                            nodeMapping.Add(node.id, node);
                        }
                        else
                        {
                            //创建节点
                            node = new OrganizationModel()
                            {
                                id = item.Organization_ID,
                                code = item.Organization_Code,
                                name = item.Organization_Name,
                                abbr = item.Organization_Abbr,
                                type = item.Organization_Type,
                                desc = null,
                                expanded = true,
                                leaf = item.IsLeaf
                            };

                            //判断是否为子节点
                            if (!item.IsLeaf)
                            {
                                //创建子节点集合
                                node.children = new List<TreeNodeModel>();
                            }

                            //添加到节点映射
                            nodeMapping.Add(node.id, node);

                            //检索父节点
                            if (item.Parent_ID.HasValue && nodeMapping.TryGetValue(item.Parent_ID.Value, out parentNode))
                            {
                                //检索父节点并且添加到父节点中
                                parentNode.children.Add(node);
                            }
                            else
                            {
                                //添加到节点表
                                nodes.Add(node);
                            }
                        }
                    }

                    //保存为组织结构模型
                    s_Organizations = nodes;
                }
            }

            //检索组织结构
            //OrganizationModel[] data = s_Organizations.Where(item => item.id == FormsAuthenticationHelper.LogonUser.Organization_ID).ToArray();

            //返回结果
            return this.TreeData(true, s_Organizations, s_Organizations.Count);
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 组织结构模型
        /// </summary>
        private static List<OrganizationModel> s_Organizations;

        #endregion
    }
}
